home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH06 / PGM6_2.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-10-10  |  2.5 KB  |  119 lines

  1. ; Simple Arithmetic
  2. ; This program demonstrates some simple arithmetic instructions.
  3.  
  4.         .386            ;So we can use extended registers
  5.         option    segment:use16    ; and addressing modes.
  6.  
  7. dseg        segment    para public 'data'
  8.  
  9. ; Some type definitions for the variables we will declare:
  10.  
  11. uint        typedef    word        ;Unsigned integers.
  12. integer        typedef    sword        ;Signed integers.
  13.  
  14.  
  15. ; Some variables we can use:
  16.  
  17. j        integer    ?
  18. k        integer    ?
  19. l        integer    ?
  20.  
  21. u1          uint    ?
  22. u2        uint    ?
  23. u3        uint    ?
  24.  
  25. dseg        ends
  26.  
  27. cseg        segment    para public 'code'
  28.         assume    cs:cseg, ds:dseg
  29.  
  30. Main        proc
  31.         mov    ax, dseg
  32.         mov    ds, ax
  33.         mov    es, ax
  34.  
  35. ; Initialize our variables:
  36.  
  37.         mov    j, 3
  38.         mov    k, -2
  39.  
  40.         mov    u1, 254
  41.         mov    u2, 22
  42.  
  43.  
  44. ; Extended multiplication using 8086 instructions.
  45. ;
  46. ; Note that there are separate multiply instructions for signed and
  47. ; unsigned operands.
  48. ;
  49. ; L := J * K (ignoring overflow)
  50.  
  51.         mov    ax, J
  52.         imul    K        ;Computes DX:AX := AX * K
  53.         mov    L, ax        ;Ignore overflow into DX.
  54.  
  55. ; u3 := u1 * u2
  56.  
  57.         mov    ax, u1
  58.         mul    u2        ;Computes DX:AX := AX * U2
  59.         mov    u3, ax        ;Ignore overflow in DX.
  60.  
  61.  
  62. ; Extended division using 8086 instructions.
  63. ;
  64. ; Like multiplication, there are separate instructions for signed
  65. ; and unsigned operands.
  66. ;
  67. ; It is absolutely imperative that these instruction sequences sign
  68. ; extend or zero extend their operands to 32 bits before dividing.
  69. ; Failure to do so will may produce a divide error and crash the
  70. ; program.
  71. ;
  72. ; L := J div K
  73.  
  74.         mov    ax, J
  75.         cwd            ;*MUST* sign extend AX to DX:AX!
  76.         idiv    K        ;AX := DX:AX/K, DX := DX:AX mod K
  77.         mov    L, ax
  78.  
  79. ; u3 := u1/u2
  80.  
  81.         mov    ax, u1
  82.         mov    dx, 0        ;Must zero extend AX to DX:AX!
  83.         div    u2        ;AX := DX:AX/u2, DX := DX:AX mod u2
  84.         mov    u3, ax
  85.  
  86. ; Special forms of the IMUL instruction available on 80286, 80386, and
  87. ; later processors.  Technically, these instructions operate on signed
  88. ; operands only, however, they do work fine for unsigned operands as well.
  89. ; Note that these instructions produce a 16-bit result and set the overflow
  90. ; flag if overflow occurs.
  91. ;
  92. ; L := J * 10 (80286 and later only)
  93.  
  94.         imul    ax, J, 10    ;AX := J*10
  95.         mov    L, ax
  96.  
  97. ; L := J * K (80386 and later only)
  98.  
  99.         mov    ax, J
  100.         imul    ax, K
  101.         mov    L, ax
  102.  
  103.  
  104.  
  105. Quit:        mov    ah, 4ch            ;DOS opcode to quit program.
  106.         int    21h            ;Call DOS.
  107. Main        endp
  108.  
  109. cseg        ends
  110.  
  111. sseg        segment    para stack 'stack'
  112. stk        byte    1024 dup ("stack   ")
  113. sseg        ends
  114.  
  115. zzzzzzseg    segment    para public 'zzzzzz'
  116. LastBytes    byte    16 dup (?)
  117. zzzzzzseg    ends
  118.         end    Main
  119.